package org.example.student_dormitory.dao.impl;

import org.example.student_dormitory.dao.StudentDao;
import org.example.student_dormitory.entity.Building;
import org.example.student_dormitory.entity.Dormitory;
import org.example.student_dormitory.entity.Student;
import org.example.student_dormitory.utils.DBUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StudentDaoImpl implements StudentDao {

    @Override
    public List<Student> selectAll(int page, int pageSize) {
        System.out.println("4已经进入Dao");
        //无条件分页
        String sql = "select s.*,d.name dormitoryName,d.buildingId buildingId,b.name buildingName from student s left join dormitory d on s.dormitoryId=d.id " +
                " left join building b on d.buildingId=b.id where s.dr=0 order by buildingName limit ?,?";
        page=(page-1)*pageSize;
        List<Student> list = new ArrayList<>();
        List<Map<String, Object>> queryList = DBUtils.queryList(sql, page, pageSize);
        if (queryList == null || queryList.size() == 0){
            return null;
        }else {
            int count = selectCount();
            System.out.println("5 66666666666");
            for (int i = 0; i < queryList.size(); i++) {
                Student student = new Student();
                student.setId(Long.parseLong(queryList.get(i).get("id").toString()));
                student.setName(queryList.get(i).get("name").toString());
                student.setNumber(queryList.get(i).get("number").toString());
                student.setGender(queryList.get(i).get("gender").toString());
                student.setDormitoryName(queryList.get(i).get("dormitoryName").toString());
                student.setDormitoryId(Long.parseLong(queryList.get(i).get("dormitoryId").toString()));
                student.setBuildingName(queryList.get(i).get("buildingName").toString());
                student.setBuildingId(Integer.parseInt(queryList.get(i).get("buildingId").toString()));
                student.setCreateDate(queryList.get(i).get("createDate").toString());
                student.setStatus(queryList.get(i).get("status").toString());
                student.setTotalCount(count);
                list.add(student);
            }
            System.out.println("6返回list给Service");
            return list;
        }
    }

    @Override
    public List<Student> selectAll(int page, int pageSize, String key, String value) {
        return null;
    }

    @Override
    public int selectCount() {
        String sql = "select count(*) from student where dr=0";
        Map<String, Object> stringObjectMap = DBUtils.qureyOne(sql);
        return Integer.parseInt(stringObjectMap.get("count(*)").toString());
    }

    @Override
    public int selectCount(String key, String value) {
        return 0;
    }

    //查询楼栋信息
    @Override
    public List<Building> selectBuildings() {
        List<Building> list = new ArrayList<>();
        String sql = "select id,name from building where dr=0";
        List<Map<String, Object>> queryList = DBUtils.queryList(sql);
        if (queryList == null || queryList.size() == 0) {
            return null;
        }else {
            for (int i = 0; i < queryList.size(); i++) {
                Building building = new Building();
                building.setId(Long.parseLong(queryList.get(i).get("id").toString()));
                building.setName(queryList.get(i).get("name").toString());
                list.add(building);
            }
            return list;
        }
    }

    @Override
    public List<Dormitory> selectByBuildingId(int buildingId) {
        //查询宿舍信息

        List<Dormitory> list = new ArrayList<>();
        String sql = "select * from dormitory where buildingId=? and sparebed !=0 and dr=0";
        List<Map<String, Object>> queryList = DBUtils.queryList(sql, buildingId);
        if (queryList == null || queryList.size() == 0){
            return null;
        }else {
//            System.out.println("5 66666666666");
            for (int i = 0; i < queryList.size(); i++) {
                Dormitory dormitory = new Dormitory();
                dormitory.setId(Long.parseLong(queryList.get(i).get("id").toString()));
                dormitory.setName(queryList.get(i).get("name").toString());
                list.add(dormitory);
            }

            return list;
        }
    }

    @Override
    public boolean insertStudent(Student student, Connection connection) {
//        System.out.println("4已经进入Dao insert");
        String sql = "insert into student(number,name,gender,dormitoryId,status,createDate,dr) values(?,?,?,?,?,?,?)";
        try {
            boolean insert = DBUtils.insert(connection,sql, student.getNumber(), student.getName(), student.getGender(),
                    student.getDormitoryId(), student.getStatus(), student.getCreateDate(), student.getDr());
//            System.out.println("5返回insert给Service");
           return insert;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean updateByDormitoryId(int dormitoryId, Connection connection) {
//        System.out.println("4已经进入Dao update");
        String sql1 = "select sparebed from dormitory where id=?";
        Map<String, Object> stringObjectMap = DBUtils.qureyOne(sql1, dormitoryId);
        int sparebed = Integer.parseInt(stringObjectMap.get("sparebed").toString());
        sparebed -= 1;

        String sql2 = "update dormitory set sparebed="+sparebed+" where id=?";
        try {
            boolean update = DBUtils.update(connection,sql2, dormitoryId);
//            System.out.println("5返回update给Service");
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
